
package com.chenjun.web.controller;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContext;

import com.chenjun.common.Constant;
import com.chenjun.common.base.ResultDto;
import com.chenjun.core.dao.generator.entity.SysUser;

/**
 * 
 * @Description: 基础控制器类
 * @author yanjinyin
 * @date 2017年7月15日 下午11:15:18
 */
@SuppressWarnings("serial")
public class BaseController implements Serializable {
	
	/**
	 * 
	 * @Description: 获取当前登陆用户
	 * @return
	 * @author yanjinyin
	 * @date 2017年7月15日 下午11:16:03
	 */
	protected SysUser getLoginUser() {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		SysUser user = (SysUser) session.getAttribute(Constant.Session.USER.toString());
		return user;
	}
	
	/**
	 * 
	 * @Description: 获取当前登陆用户ID
	 * @return
	 * @author yanjinyin
	 * @date 2017年7月15日 下午11:22:51
	 */
	protected String getLoginUserId() {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		SysUser user = (SysUser) session.getAttribute(Constant.Session.USER.toString());
		return user.getId();
	}
	
	/***
	 * 
	 * @Description: 获取当前登陆用户名称
	 * @return
	 * @author yanjinyin
	 * @date 2017年7月15日 下午11:24:16
	 */
	protected String getLoginUserName() {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		SysUser user = (SysUser) session.getAttribute(Constant.Session.USER.toString());
		return user.getUserName();
	}
	
	/**
	 * 
	 * 根据主键值获取国际化信息
	 * @param key
	 * @return
	 * String
	 */
	public static String getLocaleMessage(String key){
        HttpServletRequest request =  ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        RequestContext requestContext = new RequestContext(request);
        return requestContext.getMessage(key);
    }
	

	protected boolean hanleErrors(ResultDto rtDto, Errors errors) {
		if (!errors.hasErrors()) {
			rtDto.setType(ResultDto.SUCCESS);
			return true;
		} else {
			rtDto.setType(ResultDto.ERROR);
			List<ObjectError> allErrors = errors.getAllErrors();
			rtDto.setMessage(allErrors.get(0).getDefaultMessage());
			return false;
		}
	}
    
    /**
	 * 设置session的值
	 * 
	 * @param request
	 * @param key
	 * @param value
	 */
	public static void setAttr(HttpServletRequest request, String key,
			Object value) {
		request.getSession(true).setAttribute(key, value);
	}

	/**
	 * 获取session的值
	 * 
	 * @param request
	 * @param key
	 * @param value
	 */
	public static Object getAttr(HttpServletRequest request, String key) {
		return request.getSession(true).getAttribute(key);
	}

	/**
	 * 删除Session值
	 * 
	 * @param request
	 * @param key
	 */
	public static void removeAttr(HttpServletRequest request, String key) {
		request.getSession(true).removeAttribute(key);
	}
	
}
